import Mobel.Student;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.List;

public class EmpDaolmpl {
    private QueryRunner queryRunner = new QueryRunner();

    /*
     * 1.查询所有的数据
     * */

    public List<Student> selectAll() {

        try {
            List<Student> emps = queryRunner.query(DbUtils1.getConnection(),
                    "select* from student;",
                    new BeanListHandler<Student>(Student.class));
            return emps;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }

        return null;
    }
    /*
     * 2.实现删除的业务逻辑接口
     * */
    public int delete(int id) {
        try {
            int result = queryRunner.update(DbUtils1.getConnection(),
                    "delete from student where id=?;", id);
            return result;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return 0;
    }
    /*
     * 3.更新的业务逻辑
     * */
    public int update(Student student) {
        try {
            int result =queryRunner.update(DbUtils1.getConnection(),
                    "update student set name=?,sex=?,age=?,major=? where id=?",
                    student.getName(),student.getSex(), student.getAge(), student.getMajor(),student.getId());
            return result;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return 0;
    }
    /*
     * 选择一条数据
     * */
    public Student select(int id) {
        System.out.println("数据操作层：id = " + id);
        try {
//            Emp emp =queryRunner.query(DbUtils.getConnection(),
//                    "select * fromemp where id=?;",
//                    newBeanHandler<Emp>(Emp.class),id);

            Student student = queryRunner.query(DbUtils1.getConnection(),
                    "select* from student where id=?;",
                    new BeanHandler<Student>(Student.class), id);

            return student;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }
    /*
     * 插入一条数据
     * */
    public int insert(Student student3) {
        System.out.println("数据操作的DAO：emp.toString() = " + student3.toString());
        try {
//            int result =queryRunner.update(DbUtils.getConnection(),
//                    "update empset name=?,salary=?,age=? where id=?",
//                    emp.getName(),emp.getSalary(), emp.getAge(), emp.getId());
            int result = queryRunner.update(DbUtils1.getConnection(),
                    "insert into student(name,sex,age,major,time) values(?,?,?,?,?);",
                    student3.getName(),student3.getSex(), student3.getAge(),student3.getMajor(),student3.getTime());
            System.out.println("数据入库成功啦--result =" + result);
            return result;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return 0;
    }
}
